package com.kg.fiber.view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.border.EmptyBorder;

import com.ivan.toolkit.swing.ShadowLabel;

public class MainFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3630571891109435766L;
	
	
	private JPanel contentPane;
	private ShadowLabel shadowLabel;
	private JPanel panel;
	private JLabel label;
	private JPanel panelCenter;
	private JScrollPane scrollPane;
	private WarningManagerPanel warningManagerPanel;
	private JSplitPane splitPane;
	private LogPanel logPanel;
	private NodeListPanel nodeListPanel;
//	private DebugPanel debugPanel;
//	private MonitorPanel monitorPanel;
	
	//private boolean debug = false;

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		//setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/images/app.png")));
		setExtendedState(Frame.MAXIMIZED_BOTH);
		//this.setIconImage(Application.getIconImage());
		setTitle("网络监测软件 V1.0");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 678, 450);
		contentPane = new JPanel();
		contentPane.setFont(new Font("微软雅黑", Font.PLAIN, 12));
		contentPane.setBorder(null);
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panelTitle = new JPanel();
		panelTitle.setBorder(new EmptyBorder(0, 10, 0, 10));
		panelTitle.setBackground(new Color(119, 136, 153));
		panelTitle.setPreferredSize(new Dimension(10, 50));
		contentPane.add(panelTitle, BorderLayout.NORTH);
		GridBagLayout gbl_panelTitle = new GridBagLayout();
		gbl_panelTitle.columnWidths = new int[]{144, 0, 0, 0, 0};
		gbl_panelTitle.rowHeights = new int[]{33, 0};
		gbl_panelTitle.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
		gbl_panelTitle.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		panelTitle.setLayout(gbl_panelTitle);
		
		shadowLabel = new ShadowLabel();
		shadowLabel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.isShiftDown() && e.getClickCount() == 2) {
					switchCard();
				}
			}
		});
		shadowLabel.setFont(new Font("微软雅黑", Font.BOLD, 24));
		shadowLabel.setForeground(Color.WHITE);
		shadowLabel.setText("网络监测软件");
		GridBagConstraints gbc_shadowLabel = new GridBagConstraints();
		gbc_shadowLabel.insets = new Insets(0, 0, 0, 5);
		gbc_shadowLabel.anchor = GridBagConstraints.WEST;
		gbc_shadowLabel.gridx = 0;
		gbc_shadowLabel.gridy = 0;
		panelTitle.add(shadowLabel, gbc_shadowLabel);
		
		panel = new JPanel();
		panel.setBackground(new Color(119, 136, 153));
		contentPane.add(panel, BorderLayout.SOUTH);
		
		label = new JLabel("上海凯工 版权所有@2017");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 15));
		label.setForeground(Color.WHITE);
		panel.add(label);
		
		panelCenter = new JPanel();
		contentPane.add(panelCenter, BorderLayout.CENTER);
		panelCenter.setLayout(new CardLayout(0, 0));
		
		scrollPane = new JScrollPane();
		panelCenter.add(scrollPane, "monitor");
		
		warningManagerPanel = new WarningManagerPanel();
		scrollPane.setViewportView(warningManagerPanel);
		
		splitPane = new JSplitPane();
		splitPane.setResizeWeight(0.7);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		panelCenter.add(splitPane, "manager");
		
		logPanel = new LogPanel();
		splitPane.setRightComponent(logPanel);
		
		nodeListPanel = new NodeListPanel();
		splitPane.setLeftComponent(nodeListPanel);
		
		/*
		monitorPanel = new MonitorPanel();
		panelCenter.add(monitorPanel, "monitor");
		
		debugPanel = new DebugPanel();
		panelCenter.add(debugPanel, "debug");
		
		showDebug(false);
		*/
	}
	
	
	private void switchCard() {
		CardLayout layout = (CardLayout)panelCenter.getLayout();
		layout.next(panelCenter);
	}
	
	@Override
	protected void processWindowEvent(WindowEvent e) {
		if (e.getID() == WindowEvent.WINDOW_CLOSING) {
			int ret = JOptionPane.showConfirmDialog(null, "确定要退出系统？", "退出",
					JOptionPane.YES_NO_OPTION);
			if(ret == JOptionPane.YES_OPTION) {
				super.processWindowEvent(e);
			}
		}
	}


}
